package com.generator.gen.mysql;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.generator.gen.ColumnSelector;
import com.generator.gen.GeneratorConfig;
import com.generator.gen.TableDefinition;
import com.generator.gen.TableSelector;
import org.springframework.cglib.beans.BeanMap;

import java.util.Map;
import java.util.Objects;

import static com.generator.util.FieldUtil.convertString;

/**
 * 查询mysql数据库表
 */
public class MySqlTableSelector extends TableSelector {

	private static String  showTablesql="select table_name, table_comment, create_time, update_time from information_schema.tables\n" +
			"\t\twhere table_schema = %s";
	private static String  showTablemountsql="select count(*) as mount from information_schema.tables\n" +
			"\t\twhere table_schema = %s";

	public MySqlTableSelector(ColumnSelector columnSelector,
							  GeneratorConfig dataBaseConfig) {
		super(columnSelector, dataBaseConfig);
	}

	@Override
	protected String getShowTablesSQL(GeneratorConfig generatorConfig) {
		String dbName = generatorConfig.getDbName();
		// 兼容dbName包含特殊字符会报错的情况
		if (!(dbName.startsWith("`") && dbName.endsWith("`"))) {
			dbName = String.format("`%s`",dbName);
		}
		String sql = "SHOW TABLE STATUS FROM " + dbName;
		if(this.getSchTableNames() != null && this.getSchTableNames().size() > 0) {
			StringBuilder tables = new StringBuilder();
			for (String table : this.getSchTableNames()) {
				tables.append(",'").append(table).append("'");
			}
			sql += " WHERE NAME IN (" + tables.substring(1) + ")";
		}

		return sql;
	}

	@Override
	protected String getShowTablesSQL(GeneratorConfig generatorConfig, Page page, TableDefinition tableDefinition) {
		String dbName = generatorConfig.getDbName();
		// 兼容dbName包含特殊字符会报错的情况
		if (!(dbName.startsWith("'") && dbName.endsWith("'"))) {
			dbName = String.format("'%s'",dbName);
		}
		String sql= String.format(showTablesql, dbName);
		BeanMap beanMap = BeanMap.create(tableDefinition);
		for (Object key :beanMap.keySet()) {
			Object obj = beanMap.get(key);
			if(key.equals("tableName")&& Objects.nonNull(obj)){
				sql+=" and table_name='"+obj+"'" ;

			}
			if(key.equals("comment")&& Objects.nonNull(obj)){
				sql+=" and table_comment='"+obj+"'" ;

			}
		}
		String pageStr=" LIMIT %d, %d";
		sql+= String.format(pageStr,(page.getCurrent()-1)* page.getSize(),page.getSize() );
		return sql;
	}

	@Override
	protected String getShowTablesmountSQL(GeneratorConfig generatorConfig, TableDefinition tableDefinition) {
		String dbName = generatorConfig.getDbName();
		// 兼容dbName包含特殊字符会报错的情况
		if (!(dbName.startsWith("'") && dbName.endsWith("'"))) {
			dbName = String.format("'%s'",dbName);
		}
		String sql= String.format(showTablemountsql, dbName);
		BeanMap beanMap = BeanMap.create(tableDefinition);
		for (Object key :beanMap.keySet()) {
			Object obj = beanMap.get(key);
			if(key.equals("tableName")&& Objects.nonNull(obj)){
				sql+=" and table_name='"+obj+"'" ;

			}
			if(key.equals("comment")&& Objects.nonNull(obj)){
				sql+=" and table_comment='"+obj+"'" ;

			}
		}
		return sql;
	}

	@Override
	protected TableDefinition buildTableDefinition(Map<String, Object> tableMap) {
		TableDefinition tableDefinition = new TableDefinition();
		tableDefinition.setTableName(convertString(tableMap.get("NAME")));
		tableDefinition.setComment(convertString(tableMap.get("COMMENT")));
		return tableDefinition;
	}

	@Override
	protected TableDefinition buildPageTableDefinition(Map<String, Object> tableMap) {
		TableDefinition tableDefinition = new TableDefinition();
		tableDefinition.setTableName(convertString(tableMap.get("TABLE_NAME")));
		tableDefinition.setComment(convertString(tableMap.get("TABLE_COMMENT")));
		return tableDefinition;
	}
}
